from ultralytics import YOLO
from PIL import ImageFont
import numpy as np
from PIL import Image
import Config
import torch

model = YOLO('./best.pt', task='detect')
model(np.zeros((48, 48, 3)), device='cpu')  #预先加载推理模型
fontC = ImageFont.truetype("Font/platech.ttf", 25, 0)

results = model('./zllImg/1.jpg', conf=0.25, iou=0.7)
result_file_path = './zllImg/result1.jpg'
location_list = results[0].boxes.xyxy.tolist()
location_list_12 = [list(map(int, e)) for e in location_list]
cls_list = results[0].boxes.cls.tolist()
cls_list_12 = [int(i) for i in cls_list]
conf_list = results[0].boxes.conf.tolist()
conf_list_12 = ['%.2f %%' % (each*100) for each in conf_list]
print('------------框---------location_list=', location_list_12)
print('-------------类别序号--------cls_list=', cls_list_12)
print('-------------类别名称-----------------', results[0].names)
print('-------------置信度--------conf_list=', conf_list_12)

for r in results:
    im_array = r.plot()  # 绘制包含预测结果的BGR numpy数组
    im = Image.fromarray(im_array[..., ::-1])  # RGB PIL图像
    im.save(result_file_path)  # 保存图像